package com.momo.hr.controller.system.basic;

import com.momo.hr.bean.Menu;
import com.momo.hr.bean.RespBean;
import com.momo.hr.bean.Role;
import com.momo.hr.service.MenuService;
import com.momo.hr.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限控制管理器(前端权限组)
 */
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissController {

    @Autowired
    RoleService roleService;

    @Autowired
    MenuService menuService;

    //获取所有的权限
    @GetMapping("/")
    public List<Role> getAllRoles(){
        return this.roleService.getAllRoles();
    }

    /**
     * 获取所有菜单的
     * @return
     */
    @GetMapping("/menus")
    public List<Menu> getAllMenus(){
        return this.menuService.getAllMenusWithChildren();
    }

    //根据查询不同菜单对应不同的角色权限
    @GetMapping("mids/{rid}")
    public List<Integer> getMidByRid(@PathVariable Integer rid){
        return this.menuService.getMidByRid(rid);
    }

    /**
     * 跟新操作
     */
    @PutMapping("/")
    public RespBean updateRole(Integer rid,Integer[] mids){
        if(menuService.updateMenuRole(rid,mids)){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("跟新失败");
    }

    /**
     * 添加角色
     * @param role
     * @return RespBean
     */
    @PostMapping("/role")
    public RespBean addRole(@RequestBody Role role){
        if(roleService.addRole(role) == 1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    /**
     * 删除的方法
     * @param rid
     * @return
     */
    @DeleteMapping("/role/{rid}")
    public RespBean deleteRole(@PathVariable Integer rid){
        if(roleService.deleteRoleById(rid) == 1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
